package org.xxpay.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xxpay.core.entity.PayDomain;
import org.xxpay.core.entity.PayDomainExample;
import org.xxpay.core.service.IPayDomainService;
import org.xxpay.service.dao.mapper.PayDomainMapper;

import java.util.*;

/**
 * @author: pangxiaoyu
 * @date: 2020/01/17
 * @description:
 */
@Service
public class PayDomainServiceImpl implements IPayDomainService {

    @Autowired
    private PayDomainMapper payDomainMapper;

    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    @Override
    public int add(PayDomain payDomain) {
        return payDomainMapper.insertSelective(payDomain);
    }

    @Override
    public int update(PayDomain payDomain) {
        return payDomainMapper.updateByPrimaryKeySelective(payDomain);
    }

    @Override
    public PayDomain find(PayDomain payDomain) {
        PayDomainExample example = new PayDomainExample();
        PayDomainExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, payDomain);
        List<PayDomain> payDomainList = payDomainMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(payDomainList)) return null;
        return payDomainList.get(0);
    }


    @Override
    public List<PayDomain> select(int offset, int limit, PayDomain payDomain) {
        PayDomainExample example = new PayDomainExample();
        example.setOrderByClause("createTime DESC");
        example.setOffset(offset);
        example.setLimit(limit);
        PayDomainExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, payDomain);
        return payDomainMapper.selectByExample(example);
    }

    @Override
    public Integer count(PayDomain payDomain) {
        PayDomainExample example = new PayDomainExample();
        PayDomainExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, payDomain);
        return payDomainMapper.countByExample(example);
    }

    @Override
    public List<PayDomain> selectList(PayDomain payDomain) {
        PayDomainExample example = new PayDomainExample();
        example.setOrderByClause("createTime DESC");
        PayDomainExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, payDomain);
        return payDomainMapper.selectByExample(example);
    }

    @Override
    public PayDomain findById(Integer id) {
        return payDomainMapper.selectByPrimaryKey(id);
    }

    @Override
    public int batchDelete(String[] ids) {
        if(ids.length == 0) return 0;
        int deleteCount = 0;
        for(String id : ids) {
            deleteCount = deleteCount + payDomainMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
        return deleteCount;
    }

    @Override
    public PayDomain getRandomPayDomain(Byte... domainTypes) {
        PayDomain queryPayDomain = new PayDomain();
        queryPayDomain.setDomainStatus((byte) 1);   // 域名状态开启
        queryPayDomain.setRiskStatus((byte) 0);     // 风控状态未风控

        PayDomainExample example = new PayDomainExample();
        example.setOrderByClause("createTime desc");
        PayDomainExample.Criteria criteria = example.createCriteria();
        List dtList = new LinkedList();
        for(int i=0; i<domainTypes.length; i++) {
            dtList.add(domainTypes[i]);
        }
        criteria.andDomainTypeIn(dtList);
        setCriteria(criteria, queryPayDomain);
        List<PayDomain> domainList = payDomainMapper.selectByExample(example);

        if(CollectionUtils.isEmpty(domainList)) {
            return null;
        }

        // 随机排序
        Collections.shuffle(domainList);

        return domainList.get(0);
    }

    void setCriteria(PayDomainExample.Criteria criteria, PayDomain payDomain) {
        if(payDomain != null) {
            if(payDomain.getRiskStatus() != null && payDomain.getRiskStatus() != -99) criteria.andRiskStatusEqualTo(payDomain.getRiskStatus());
            if(payDomain.getDomainStatus() != null && payDomain.getDomainStatus() != -99) criteria.andDomainStatusEqualTo(payDomain.getDomainStatus());
            if(StringUtils.isNotEmpty(payDomain.getDomainUrl())) criteria.andDomainUrlLike("%" + payDomain.getDomainUrl() + "%");
        }
    }
}
